from flask import Blueprint, jsonify, request
import random

class Plugin:
    def __init__(self):
        self.blueprint = Blueprint('math_practice', __name__)
        
    def init_app(self, app):
        @self.blueprint.route('/check', methods=['POST'])
        def check_answers():
            answers = request.json.get('answers', [])
            correct_answers = request.json.get('correct_answers', [])
            
            if len(answers) != len(correct_answers):
                return jsonify({
                    'error': '答案数量不匹配'
                }), 400
                
            correct_count = sum(1 for a, b in zip(answers, correct_answers) if float(a) == float(b))
            score = (correct_count / len(answers)) * 100
            
            feedback = self._get_feedback(score)
            
            return jsonify({
                'score': score,
                'correct_count': correct_count,
                'total': len(answers),
                'feedback': feedback
            })
            
        app.register_blueprint(self.blueprint, url_prefix='/api/math_practice')
        
    def _get_feedback(self, score):
        if score == 100:
            return {
                'emoji': '🌟',
                'message': '太棒了！满分！'
            }
        elif score >= 80:
            return {
                'emoji': '😊',
                'message': '做得很好！'
            }
        elif score >= 60:
            return {
                'emoji': '🙂',
                'message': '及格了，继续加油！'
            }
        else:
            return {
                'emoji': '😔',
                'message': '要多加练习哦！'
            }
            
    def enable(self):
        pass
        
    def disable(self):
        pass 